exports.__esModule = true;

var _jsonwebtoken = require('jsonwebtoken');

var _jsonwebtoken2 = _interopRequireDefault(_jsonwebtoken);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.BaseController {
    indexAction() {}
    loginAction() {
        var _this = this;

        return _asyncToGenerator(function* () {
            //用户登录
            const data1 = _this.post('username,password');
            const data2 = {
                login_ip: _this.ctx.ip,
                last_login_time: new Date()
            };
            let aj = think.ajaxJson();
            const UserService = _this.service('api/sys/UserService');
            const res = yield UserService.isLogin(data1, data2);
            if (res.error) {
                aj.success = false;
                aj.msg = '数据库查询错误';
            } else if (res.msg) {
                aj.success = false;
                aj.msg = res.msg;
            } else {
                //    const content = {userId:res.id};
                //    const tokenConfig = think.config('token');
                //    const PrivateKey = tokenConfig.privateKey; // （密钥）
                //    const expiresIn = tokenConfig.expiresIn;
                //    const token = jwt.sign(content, PrivateKey, {
                //        expiresIn: expiresIn // 过期时间
                //    });
                //    aj.result = res;
                //    aj.attributes.token = token;
                aj.result = res.UserInfo;
                aj.msg = '登陆成功';
                aj.attributes.token = res.token;
                _this.ctx.cookie('access_token', res.token);
            }
            return _this.json(aj);
        })();
    }

    doUploadImgAction() {
        var _this2 = this;

        return _asyncToGenerator(function* () {
            //用户头像上传
            const file = _this2.file('image');
            const userid = _this2.ctx.header.userid;
            const serverIP = 'http://' + think.config('staticIp') + ':' + think.config('proxyPort');

            let aj = think.ajaxJson();
            if (file) {
                //保存图片
                const pathObj = yield _this2.saveFile(file);
                if (pathObj) {
                    const cutImg = yield _this2.cutImg(think.resource + '/' + pathObj.filePathStatic);
                    if (cutImg) {
                        const UserService = _this2.service('api/sys/UserService');
                        const upfiled = { pic_path: pathObj.filePathStatic };
                        const res = yield UserService.saveUserInfo(upfiled, userid);
                        if (!res.error) {
                            aj.msg = '用户头像更新成功';
                            aj.result = {
                                fileName: pathObj.fileName,
                                filePathStatic: serverIP + '/' + pathObj.filePathStatic
                            };
                        } else {
                            aj.success = false;
                            aj.msg = '用户头像信息更新失败';
                        }
                    } else {
                        aj.success = false;
                        aj.msg = '图片裁切失败';
                    }
                } else {
                    aj.success = false;
                    aj.msg = '图片保存失败';
                }
            } else {
                aj.success = false;
                aj.msg = "文件上传失败";
            }
            return _this2.json(aj);
        })();
    }

    saveUserInfoAction() {
        var _this3 = this;

        return _asyncToGenerator(function* () {
            //修改用户信息
            let aj = think.ajaxJson();
            const userid = _this3.post('UserID');
            let data = {};
            data[_this3.post('Key')] = _this3.post('Value');
            const UserService = _this3.service('api/sys/UserService');
            const res = yield UserService.saveUserInfo(data, userid);
            if (res && res.error) {
                aj.success = false;
                aj.msg = '用户信息修改失败';
            }
            return _this3.json(aj);
        })();
    }

    changePsdAction() {
        var _this4 = this;

        return _asyncToGenerator(function* () {
            //修改密码
            let aj = think.ajaxJson();
            const data = _this4.post('UserID,OldPsd,NewPsd');
            const UserService = _this4.service('api/sys/UserService');
            const res = yield UserService.changePsd(data);
            if (!res.error) {
                if (res && res.msg) {
                    aj.success = false;
                    aj.msg = res.msg;
                }
            } else {
                aj.success = false;
                aj.msg = '用户密码修改失败';
            }
            return _this4.json(aj);
        })();
    }

    getUserInfoAction() {
        var _this5 = this;

        return _asyncToGenerator(function* () {
            //获取用户详情
            let aj = think.ajaxJson();
            const data = { UserID: _this5.post('UserID') };
            const UserService = _this5.service('api/sys/UserService');
            const res = yield UserService.getUserInfo(data);
            if (!res.error) {
                if (res && res.msg) {
                    aj.success = false;
                    aj.msg = res.msg;
                } else {
                    aj.result = res;
                }
            } else {
                aj.success = false;
                aj.msg = '获取用户详情失败';
            }
            return _this5.json(aj);
        })();
    }

    getUserListByRnameAction() {
        var _this6 = this;

        return _asyncToGenerator(function* () {
            //按真实姓名模糊分页查询
            let aj = think.ajaxJson();
            const data = {
                page: Number(_this6.post('page')),
                pageSize: Number(_this6.post('pageSize')),
                realname: _this6.post('realname')
            };
            const UserService = _this6.service('api/sys/UserService');
            const res = yield UserService.getUserListByRname(data);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '获取用户列表失败';
            }
            return _this6.json(aj);
        })();
    }
    userTaskCountAction() {
        var _this7 = this;

        return _asyncToGenerator(function* () {
            //获取用户各类工作统计信息
            let aj = think.ajaxJson();
            const data = {
                userId: _this7.post('userId')
            };
            const UserService = _this7.service('api/sys/UserService');
            const res = yield UserService.getUserTaskCount(data);
            if (!res.error) {
                aj.result = res;
            } else {
                aj.success = false;
                aj.msg = '获取用户待办工作统计信息失败';
            }
            return _this7.json(aj);
        })();
    }
};